//
//  CYLifePhotoVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/5/23.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYLifePhotoVC.h"
#import "LEUnitView+LE.h"
#import "CYCreditTool.h"
#import "CustomIOSAlertView.h"
#import "UIImageView+WebCache.h"
#import "MWPhotoBrowser.h"
#import "LEPhotoBrowserTool.h"
#import "CYLoginHttpTool.h"
#import "CYUser.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "UIButton+WebCache.h"



@interface CYLifePhotoVC ()
@property (weak, nonatomic) IBOutlet UIView *photoselectView;
@property (nonatomic, strong) NSMutableArray *photoUrls;
@property (weak, nonatomic) IBOutlet UILabel *shenghuorecordLab;
@property (weak, nonatomic) IBOutlet UIView *shenghuorecordView;
@property (weak, nonatomic) IBOutlet UIButton *shilitu1Btn;
@property (weak, nonatomic) IBOutlet UIButton *shilitu2Btn;


@property (nonatomic, strong) NSMutableArray *shenghuorecordArray;
//@property (weak,nonatomic) NSMutableArray *shenghuoshowArray;

@end

@implementation CYLifePhotoVC

- (void)viewDidLoad {
    [super viewDidLoad];
     
    http://www.credit-manage.com:8058/app/images/levelRule.jpg
     
     [_shilitu1Btn sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/prove/shenghuozhao1.jpg"] forState:UIControlStateNormal];
     [_shilitu2Btn sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/prove/shenghuozhao2.jpg"] forState:UIControlStateNormal];
     
     _photoUrls = [[NSMutableArray alloc]init];
     _shenghuorecordArray = [[NSMutableArray alloc]init];
     
     [self setupUploadView];
     [self setimagecerIDView];
     
    // Do any additional setup after loading the view from its nib.
}
- (void)setimagecerIDView{
     // image 前缀
     NSString *preFixString = @"http://www.credit-manage.com/";
     // 屏幕的宽度 (也是承载imageView的view的宽度)
     CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
     // image各个边界的间隔
     CGFloat space = 10.f;
     // imageview的width (设定为5张)
     CGFloat imgWidth = (screenWidth - space * 6 ) / 5;
     // imageview的height
     CGFloat imgHeight = _shenghuorecordView.frame.size.height - space * 2;
     // 由于是xib + 手码, 代码获取xib控件的frame 必须在主线程中获取
     dispatch_async(dispatch_get_main_queue(), ^{
          CYUser *user = [CYLoginHttpTool userData].user;
          [CYHttpTool postWithURL:[CYURL shared].getProveInfoPics params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
               //        CYLog(@"json = %@",json);
               CYJsonInfo *jsoninf = [CYJsonInfo objectWithKeyValues:json];
               NSMutableArray *data = jsoninf.data;
               for (NSDictionary *dic in data) {
                    // 取网络	图片地址
                    NSString *provePicAddress = [dic objectForKey:@"provePicAddress"];
                    [_shenghuorecordArray addObject:provePicAddress];
                    for (int i = 0; i < _shenghuorecordArray.count; i++ ) {
                         // image的url string
                         NSString *imgUrlString = [_shenghuorecordArray objectAtIndex:i];
                         imgUrlString = [preFixString stringByAppendingString:imgUrlString];
                         // 创建imageView
                         UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(space + (imgWidth + space) * i, space, imgWidth, imgHeight)];
                         imgView.backgroundColor = [UIColor whiteColor];
                         // 此处你可使用SDWebImage来加载
                         [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrlString]];
                         [_shenghuorecordView addSubview:imgView];
                    }
                    if (_shenghuorecordArray.count){
                         _shenghuorecordLab.hidden = YES;
                         UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoBrowser:)];
                         [_shenghuorecordView addGestureRecognizer:tag];
                    }
               }
               
          } failure:^(NSError *error) {
               
          }];
     });

}

/**
 *  图片浏览器
 */
- (void)photoBrowser:(UITapGestureRecognizer *)tag {
     NSArray *urls = _shenghuorecordArray;
     MWPhoto *photo = nil;
     NSMutableArray *photos = [NSMutableArray array];
     for (NSString *url in urls) {
          photo = [MWPhoto photoWithURL:CYURLWithService(url)];
          //        photo.caption = @"title";
          [photos addObject:photo];
     }
     [LEPhotoBrowserTool showPhotoBrowserWithController:self photos:photos];
}

- (void)setupUploadView {
     //生活照
     [LEUnitView imageScrollView:self.photoselectView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
               [_photoUrls addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
}


- (IBAction)shilitu1Click:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/prove/shenghuozhao1.jpg"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}

- (IBAction)shilitu2Click:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/prove/shenghuozhao2.jpg"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];
     
}

- (IBAction)SureClick:(id)sender {
     NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:_photoUrls,@"url", nil];
     //创建通知
     NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dict];
     //通过通知中心发送通知
     [[NSNotificationCenter defaultCenter] postNotification:notification];
     if (_photoUrls.count<3) {
          CYAlertView(@"生活照不得少于3张");
     }else{
          [self.navigationController popViewControllerAnimated:YES];
          
     }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
